<?php
$this->breadcrumbs=array(
	'Archive' => array('/archive'),
	'Board' => array('/archiveBoard'),
	'Import'
);
?>

<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/jquery.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/jqueryui.js"></script>
<link rel="stylesheet" href="<?php echo Yii::app()->baseUrl; ?>/css/redmond/jqueryui.css" />
<h1>Archive Board Import</h1>

<div id="progress"></div>
	<?php
Yii::import("application.website.*");
Yii::import("application.website.adapter.*");
Yii::import("application.models.ArchiveUrl");
Yii::import("application.views.boardImport.jqueryui.js.*");
Yii::import("application.views.boardImport.jqueryui.css.redmond.*");

$stats = ArchiveUrl::getStatistics();
if ($stats['total'] > 0) echo "<div id='stats'>".($stats['total']-$stats['unfetched'])." von ".$stats['total']." abgeschlossen - ".((100/$stats['total'])*($stats['total']-$stats['unfetched']))."%</div>";
else echo "<div id='stats'></div>";
echo "<br>";
echo "<div id='count'></div>";
echo "<div id='lastURL'>Laden...</div>";
?>

<div id="context"></div>
<div id="data" style="display: none"></div>

<script type="text/javascript">
	function load() {
		
		if ($('#data').text() != "") {
			total = parseInt($("div#data div#statistics div#total").text());
			unfetched = parseInt($("div#data div#statistics div#unfetched").text());
			categories = parseInt($("div#data div#statistics div#categories").text());
			threads = parseInt($("div#data div#statistics div#threads").text());
			posts = parseInt($("div#data div#statistics div#posts").text());
			url = $("div#content div#url").text();
			percent = ((100/total)*(total-unfetched));
			$('#progress').progressbar({
				value: percent
			});
			
			if (!isNaN(total)) $('#stats').text((total-unfetched)+" von "+total+" abgeschlossen - "+percent+"%");
			$('#count').html("Kategorien: "+categories+"<br>Themen: "+threads+"<br>Beiträge: "+posts);
			$('#lastURL').html(url);
		}
		
		$.ajax({
			url: "<?php echo Yii::app()->createUrl("archiveBoardImport/ajax")?>",
			success: function(data) {
				$('#unfetched').text((parseInt($('#unfetched').text())+1));
				$('#data').hide();
				$('#data').html(data);
				load();
			},
			error: function() {
				$('#unfetched').text((parseInt($('#unfetched').text())+1));
				$('#data').hide();
				$('#data').html("");
				load();
			}
		});
	}
	
	$(window).ready(function() {
		$('#progress').progressbar({
			value: 0
		});
		load();
	});
</script>